package lesson.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lesson.entity.NocvData;
import lesson.service.IndexService;
import lesson.vo.DataView;
import lesson.vo.NocvDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * @Description:
 * @Author: Wanzhen
 * @Date: 2022/6/10-22:29
 */
@RestController
public class ChinaDataAdminController {

    @Autowired
    private IndexService indexService;

    //模糊查询，带有分页
    @GetMapping("/listDataByPage")
    public DataView listDataByPage(NocvDataVo nocvDataVo){
        //1.创建分页的对象，当前页，每页限制大小
        IPage<NocvData> page = new Page<>(nocvDataVo.getPage(), nocvDataVo.getLimit());
        //2.创建模糊查询条件
        QueryWrapper<NocvData> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(!(nocvDataVo.getName() == null), "name", nocvDataVo.getName());
        //3.疫情数据确诊最多的排在最上面
        queryWrapper.orderByDesc("value");
        //4.查询数据库
        indexService.page(page, queryWrapper);
        //5.返回数据封装
        DataView dataView = new DataView(page.getTotal(), page.getRecords());
        return dataView;
    }

    /**
     * 根据id删除数据
     * @param id
     * @return
     */
    @DeleteMapping("/china/deleteById")
    public DataView deleteById(Integer id){
        indexService.removeById(id);
        DataView dataView = new DataView();
        dataView.setCode(200);
        dataView.setMsg("删除中国疫情数据地图成功");
        return dataView;
    }

    /**
     * 新增或修改ID
     * nocvData 有id值就是修改，没有id值就是新增
     * update nocv_data set name = "" where id = ?
     * insert into
     * @param nocvData
     * @return
     */
    @PostMapping("/china/addOrUpdateChina")
    public DataView addOrUpdateChina(NocvData nocvData){
        boolean save = indexService.saveOrUpdate(nocvData);
        DataView dataView = new DataView();
        if(save){
            dataView.setCode(200);
            dataView.setMsg("添加或新增疫情数据成功");
            return dataView;
        }
        dataView.setCode(100);
        dataView.setMsg("添加或新增疫情数据失败");
        return dataView;
    }

}
